gen firm = int((mod(_n-1, 18120))/120) +1
sort firm
by firm: egen trend = seq (), f(1) t(120)

tsset firm trend
 
* Buy initiated trades==1 (positive returns) sell==0 (negative returns)
gen buy=cds_return>0 
gen buy_equity=equity_return>0
* Buy or sell initated trade based on CDS up or down. 
// Up CDS==buy-inititaed==1 => Typically indicates that demand for protection is increasing
// Down CDS ==sell-initiated==0 => Often suggests more supply of protection

gen trade=cds>71.835
gen trade_equity=stock>44.635


* Large investors==1 Extrapolators==0
gen large= volume>18.01
gen large_equity= volume_equity>18.20951
*_______________________________________________________________________________
* Sentiment proxy: sentiment==jumps+cojumps;

reg cds_return rjv cojumps 
predict fitted
rename fitted sentiment

*_______________________________________________________________________________
*------------- Table 2 ---------------------------------------------------------
*              Panel A Predictive

reg volume L(1/3).sentiment, vce(cluster firm)
reg volume L(1/3).sentiment equity_return leverage liquidity  term , vce(cluster firm)
reg sentiment L(1/3).volume, vce(cluster firm)
reg sentiment L(1/3).volume equity_return leverage liquidity  term , vce(cluster firm)

*-------------Panel B with GMM style -------------------------------------------

pvar sentiment volume equity_return leverage liquidity term   , fod instlags(1/3) gmmstyle


*------------------- Instrimental Variable Panel C of Table 2 ------------------

gen l_cdx_spread = l.cdx_spread

reg cdx_spread l_cdx_spread
predict cdx_shock, resid

gen l_cdx_shock = l.cdx_shock

gen l_negative_news = l.negative_news

* --- First-Stage Regression (Testing IV Relevance) -----

reg sentiment cdx_shock rcv equity_return leverage liquidity term sp500 i.firm i.trend,  vce(robust)

reg sentiment cdx_shock negative_news rcv equity_return leverage liquidity term sp500 i.firm i.trend,  vce(robust)

* Second-Stage Regression (Testing Causal Effect on Trading Volume) Use both CDX shocks and negative news ------

ivreg2 volume (sentiment = cdx_shock ) equity_return leverage liquidity term sp500 i.firm, cluster(firm)

*ivreg2 volume (sentiment = cdx_shock negative_news) equity_return leverage liquidity term sp500 i.firm, cluster(firm)


* Third-Stage Overidentification Test (Ensuring Exogeneity)

ivreg2 volume (sentiment = cdx_shock) equity_return leverage liquidity term sp500 i.firm, cluster(firm) overid
*ivreg2 volume (sentiment = cdx_shock negative_news) equity_return leverage liquidity term sp500 i.firm, cluster(firm) overid


*________________________________________________________
* --------- New Table 3 with extrpoalators & buy/sell
// Increase in CDS Spread: Typically indicates that demand for protection is increasing, likely reflecting buy-initiated trades. A widening spread suggests the market views the underlying credit as riskier.
// Decrease in CDS Spread: Often suggests more supply of protection, indicating sell-initiated trades. A narrowing spread reflects lower perceived credit risk or a greater willingness to sell protection.

// Trading Volume and Depth: Sudden spikes in trading volume in combination with spread movement can provide clues. For instance, if there's a spike in volume alongside a widening spread, this usually indicates strong buy interest in credit protection.

*-- trade > median of CDS spreads in my sample trade==1 buy-initiated 
*--- trade1 > mdian of trading volume
*-- controlling for what happens to volume when Jumps occure during high CDS spreads 
 
xtreg volume L(1/3)sentiment if large ==1, fe
xtreg volume L(1/3)sentiment if large ==0, fe
xtreg volume L(1/3)sentiment if trade==1, fe

//----------- Equity ---------------------------
xtreg volume_equity L(1/3)rjv_equity if large_equity ==1, fe
xtreg volume_equity L(1/3)rjv_equity if large_equity ==0, fe
xtreg volume_equity L(1/3)rjv_equity if trade_equity==1, fe

// a) Following Jumps what happens to trading when considering only Large instituions
// b) Following Jumps what happens to trading when considering only Extrapolators
// c) Following Jumps what happens to trading when considering only the demand for protection

